<?php
/**
月指标管理
**/


namespace Apps\Controller;
use Think\Controller;
use Think\Model;

class TargetController extends Controller {

    /**
    */
    public function index(){
        $Model = new Model();
        $rq = date("Ym");
        
        $sql = "select a.dict_group,a.dict_value FROM gr_ec.gr_dict a WHERE a.dict_key = ".$rq;
        $list = $Model -> query($sql);
        
        for ($i = 0; $i < count($list); $i++) {
            if ($list[$i]['dict_group'] == "果然销售指标") {
                $grxs = $list[$i]['dict_value'];
            }
            if ($list[$i]['dict_group'] == "世果销售指标") {
                $sgxs = $list[$i]['dict_value'];
            }
            if ($list[$i]['dict_group'] == "京华城店销售指标") {
                $jhc = $list[$i]['dict_value'];
            }
            if ($list[$i]['dict_group'] == "梅岭店销售指标") {
                $ml = $list[$i]['dict_value'];
            }
            if ($list[$i]['dict_group'] == "望月路店销售指标") {
                $wy = $list[$i]['dict_value'];
            }
            if ($list[$i]['dict_group'] == "三盛店销售指标") {
                $ss = $list[$i]['dict_value'];
            }
            if ($list[$i]['dict_group'] == "时代店销售指标") {
                $sd = $list[$i]['dict_value'];
            }
            if ($list[$i]['dict_group'] == "虹桥坊店销售指标") {
                $hjf = $list[$i]['dict_value'];
            }
            if ($list[$i]['dict_group'] == "彩虹汇店销售指标") {
                $chh = $list[$i]['dict_value'];
            }
        }
        
        
        
        
        $sql = "select a.* from gr_erp.gr_logistics_param a where a.month = ".$rq;
        $list = $Model -> query($sql);
        
        for ($i = 0; $i < count($list); $i++) {
                $pja1 = $list[$i]['A1'];
                $pja2 = $list[$i]['A2'];
                $pja3 = $list[$i]['A3'];
                $pjb1 = $list[$i]['B1'];
                $pjb2 = $list[$i]['B2'];
                $pjb3 = $list[$i]['B3'];
        }
        
        
        $sql = "select a.* from gr_erp.gr_wlzb a where a.month = ".$rq." and a.category = '月度交易额指标'";
        $list = $Model -> query($sql);
        
        for ($i = 0; $i < count($list); $i++) {
                $jyea1 = $list[$i]['A1'];
                $jyea2 = $list[$i]['A2'];
                $jyea3 = $list[$i]['A3'];
                $jyeb1 = $list[$i]['B1'];
                $jyeb2 = $list[$i]['B2'];
                $jyeb3 = $list[$i]['B3'];
                $jyew1 = $list[$i]['W1'];
        }
        
        
        $sql = "select a.* from gr_erp.gr_wlzb a where a.month = ".$rq." and a.category = '月度新客数指标'";
        $list = $Model -> query($sql);
        
        for ($i = 0; $i < count($list); $i++) {
                $xyha1 = $list[$i]['A1'];
                $xyha2 = $list[$i]['A2'];
                $xyha3 = $list[$i]['A3'];
                $xyhb1 = $list[$i]['B1'];
                $xyhb2 = $list[$i]['B2'];
                $xyhb3 = $list[$i]['B3'];
                $xyhw1 = $list[$i]['W1'];
        }
        
        $this->assign('_rq', $rq);
        $this->assign('_grxs', $grxs);
        $this->assign('_sgxs', $sgxs);
        $this->assign('_jhc', $jhc);
        $this->assign('_ml', $ml);
        $this->assign('_wy', $wy);
        $this->assign('_ss', $ss);
        $this->assign('_sd', $sd);
        $this->assign('_hjf', $hjf);
        $this->assign('_chh', $chh);
        $this->assign('_pja1', $pja1);
        $this->assign('_pja2', $pja2);
        $this->assign('_pja3', $pja3);
        $this->assign('_pjb1', $pjb1);
        $this->assign('_pjb2', $pjb2);
        $this->assign('_pjb3', $pjb3);
        
        $this->assign('_jyea1', $jyea1);
        $this->assign('_jyea2', $jyea2);
        $this->assign('_jyea3', $jyea3);
        $this->assign('_jyeb1', $jyeb1);
        $this->assign('_jyeb2', $jyeb2);
        $this->assign('_jyeb3', $jyeb3);
        $this->assign('_jyew1', $jyew1);
        
        
        $this->assign('_xyha1', $xyha1);
        $this->assign('_xyha2', $xyha2);
        $this->assign('_xyha3', $xyha3);
        $this->assign('_xyhb1', $xyhb1);
        $this->assign('_xyhb2', $xyhb2);
        $this->assign('_xyhb3', $xyhb3);
        $this->assign('_xyhw1', $xyhw1);
                
                
        $this->display();
    }
    
    public function save($rq,$grxs,$sgxs,$jhc,$ml,$wy,$ss,$sd,$hjf,$chh,$pja1,$pja2,$pja3,$pjb1,$pjb2,$pjb3,$jyea1,$jyea2,$jyea3,$jyeb1,$jyeb2,$jyeb3,$jyew1,$xyha1,$xyha2,$xyha3,$xyhb1,$xyhb2,$xyhb3,$xyhw1){
        $Model = new Model();
        
        if(!$rq){
            $this->error("未获取到月份,请检查网络是否正常!");
        }
        
        //果然线上
        if($grxs){
            $sql = "select * from gr_ec.gr_dict a where a.dict_key = '".$rq."' and a.dict_group = '果然销售指标'";
            $list = $Model -> query($sql);
            if(count($list) > 0){
                //更新
                $sql = "update gr_ec.gr_dict set dict_value = '".$grxs."' where dict_key = '".$rq."' and dict_group = '果然销售指标'";
                $Model->execute($sql);
            }else{
                //插入
                $sql = "insert into gr_ec.gr_dict (dict_key,dict_group,dict_value) select '".$rq."','果然销售指标','".$grxs."'";
                $Model->execute($sql);
            }
        }
        
        //世果线上
        if($sgxs){
            $sql = "select * from gr_ec.gr_dict a where a.dict_key = '".$rq."' and a.dict_group = '世果销售指标'";
            $list = $Model -> query($sql);
            if(count($list) > 0){
                //更新
                $sql = "update gr_ec.gr_dict set dict_value = '".$sgxs."' where dict_key = '".$rq."' and dict_group = '世果销售指标'";
                $Model->execute($sql);
            }else{
                //插入
                $sql = "insert into gr_ec.gr_dict (dict_key,dict_group,dict_value) select '".$rq."','世果销售指标','".$sgxs."'";
                $Model->execute($sql);
            }
        }
        
        //京华城店
        if($jhc){
            $sql = "select * from gr_ec.gr_dict a where a.dict_key = '".$rq."' and a.dict_group = '京华城店销售指标'";
            $list = $Model -> query($sql);
            if(count($list) > 0){
                //更新
                $sql = "update gr_ec.gr_dict set dict_value = '".$jhc."' where dict_key = '".$rq."' and dict_group = '京华城店销售指标'";
                $Model->execute($sql);
            }else{
                //插入
                $sql = "insert into gr_ec.gr_dict (dict_key,dict_group,dict_value) select '".$rq."','京华城店销售指标','".$jhc."'";
                $Model->execute($sql);
            }
        }
        
        //梅岭店
        if($ml){
            $sql = "select * from gr_ec.gr_dict a where a.dict_key = '".$rq."' and a.dict_group = '梅岭店销售指标'";
            $list = $Model -> query($sql);
            if(count($list) > 0){
                //更新
                $sql = "update gr_ec.gr_dict set dict_value = '".$ml."' where dict_key = '".$rq."' and dict_group = '梅岭店销售指标'";
                $Model->execute($sql);
            }else{
                //插入
                $sql = "insert into gr_ec.gr_dict (dict_key,dict_group,dict_value) select '".$rq."','梅岭店销售指标','".$ml."'";
                $Model->execute($sql);
            }
        }
        
        //望月店
        if($wy){
            $sql = "select * from gr_ec.gr_dict a where a.dict_key = '".$rq."' and a.dict_group = '望月路店销售指标'";
            $list = $Model -> query($sql);
            if(count($list) > 0){
                //更新
                $sql = "update gr_ec.gr_dict set dict_value = '".$wy."' where dict_key = '".$rq."' and dict_group = '望月路店销售指标'";
                $Model->execute($sql);
            }else{
                //插入
                $sql = "insert into gr_ec.gr_dict (dict_key,dict_group,dict_value) select '".$rq."','望月路店销售指标','".$wy."'";
                $Model->execute($sql);
            }
        }
        
        //三盛店
        if($ss){
            $sql = "select * from gr_ec.gr_dict a where a.dict_key = '".$rq."' and a.dict_group = '三盛店销售指标'";
            $list = $Model -> query($sql);
            if(count($list) > 0){
                //更新
                $sql = "update gr_ec.gr_dict set dict_value = '".$ss."' where dict_key = '".$rq."' and dict_group = '三盛店销售指标'";
                $Model->execute($sql);
            }else{
                //插入
                $sql = "insert into gr_ec.gr_dict (dict_key,dict_group,dict_value) select '".$rq."','三盛店销售指标','".$ss."'";
                $Model->execute($sql);
            }
        }
        
        //三盛店
        if($sd){
            $sql = "select * from gr_ec.gr_dict a where a.dict_key = '".$rq."' and a.dict_group = '时代店销售指标'";
            $list = $Model -> query($sql);
            if(count($list) > 0){
                //更新
                $sql = "update gr_ec.gr_dict set dict_value = '".$sd."' where dict_key = '".$rq."' and dict_group = '时代店销售指标'";
                $Model->execute($sql);
            }else{
                //插入
                $sql = "insert into gr_ec.gr_dict (dict_key,dict_group,dict_value) select '".$rq."','时代店销售指标','".$sd."'";
                $Model->execute($sql);
            }
        }
        
        //三盛店
        if($hjf){
            $sql = "select * from gr_ec.gr_dict a where a.dict_key = '".$rq."' and a.dict_group = '虹桥坊店销售指标'";
            $list = $Model -> query($sql);
            if(count($list) > 0){
                //更新
                $sql = "update gr_ec.gr_dict set dict_value = '".$hjf."' where dict_key = '".$rq."' and dict_group = '虹桥坊店销售指标'";
                $Model->execute($sql);
            }else{
                //插入
                $sql = "insert into gr_ec.gr_dict (dict_key,dict_group,dict_value) select '".$rq."','虹桥坊店销售指标','".$hjf."'";
                $Model->execute($sql);
            }
        }
        
        //彩虹汇店
        if($chh){
            $sql = "select * from gr_ec.gr_dict a where a.dict_key = '".$rq."' and a.dict_group = '彩虹汇店销售指标'";
            $list = $Model -> query($sql);
            if(count($list) > 0){
                //更新
                $sql = "update gr_ec.gr_dict set dict_value = '".$chh."' where dict_key = '".$rq."' and dict_group = '彩虹汇店销售指标'";
                $Model->execute($sql);
            }else{
                //插入
                $sql = "insert into gr_ec.gr_dict (dict_key,dict_group,dict_value) select '".$rq."','彩虹汇店销售指标','".$chh."'";
                $Model->execute($sql);
            }
        }
        
        //派件
        $logistics_param = M('logistics_param');
        $list = $logistics_param->where(array('month' => $rq))->select();
        if(count($list) > 0){
            //更新
            $data['A1'] = $pja1;
            $data['A2'] = $pja2;
            $data['A3'] = $pja3;
            $data['B1'] = $pjb1;
            $data['B2'] = $pjb2;
            $data['B3'] = $pjb3;
            $logistics_param->where(array('month' => $rq))->save($data); // 根据条件更新记录
        }else{
            //插入
            $data['month'] = $rq;
            $data['A1'] = $pja1;
            $data['A2'] = $pja2;
            $data['A3'] = $pja3;
            $data['B1'] = $pjb1;
            $data['B2'] = $pjb2;
            $data['B3'] = $pjb3;
            $logistics_param->data($data)->add();
        }
        
        //交易额
        $Wlzb = M('Wlzb');
        $list = $Wlzb->where(array('month' => $rq,'category' => '月度交易额指标'))->select();
        if(count($list) > 0){
            //更新
            $data['A1'] = $jyea1;
            $data['A2'] = $jyea2;
            $data['A3'] = $jyea3;
            $data['B1'] = $jyeb1;
            $data['B2'] = $jyeb2;
            $data['B3'] = $jyeb3;
            $data['W1'] = $jyew1;
            $Wlzb->where(array('month' => $rq,'category' => '月度交易额指标'))->save($data); // 根据条件更新记录
        }else{
            //插入
            $data['month'] = $rq;
            $data['category'] = '月度交易额指标';
            $data['A1'] = $jyea1;
            $data['A2'] = $jyea2;
            $data['A3'] = $jyea3;
            $data['B1'] = $jyeb1;
            $data['B2'] = $jyeb2;
            $data['B3'] = $jyeb3;
            $data['W1'] = $jyew1;
            $Wlzb->data($data)->add();
        }
        
        //新用户
        $list = $Wlzb->where(array('month' => $rq,'category' => '月度新客数指标'))->select();
        if(count($list) > 0){
            //更新
            $data['A1'] = $xyha1;
            $data['A2'] = $xyha2;
            $data['A3'] = $xyha3;
            $data['B1'] = $xyhb1;
            $data['B2'] = $xyhb2;
            $data['B3'] = $xyhb3;
            $data['W1'] = $xyhw1;
            $Wlzb->where(array('month' => $rq,'category' => '月度新客数指标'))->save($data); // 根据条件更新记录
        }else{
            //插入
            $data['month'] = $rq;
            $data['category'] = '月度新客数指标';
            $data['A1'] = $xyha1;
            $data['A2'] = $xyha2;
            $data['A3'] = $xyha3;
            $data['B1'] = $xyhb1;
            $data['B2'] = $xyhb2;
            $data['B3'] = $xyhb3;
            $data['W1'] = $xyhw1;
            $Wlzb->data($data)->add();
        }
        
        $this->success('操作成功！');
    }
  }


